package edu.uoc.tdp.pac4.client.maintenance;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import edu.uoc.tdp.pac4.beans.Aula;
import edu.uoc.tdp.pac4.beans.TipusAula;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.MantenimentInterface;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

public class ClassroomSearchWindow extends JPanel {

	private static final long serialVersionUID = 1L;

	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	private ClientConnectionHelper connectionHelper_;
	private MantenimentInterface maintenance;

	/**
	 * 
	 */
	private JTextField tfId;
	private JTextField tfName;
	private JTextField tfCapacity;
	private JTable tbClassroomList;
	private JTextField tfClassroomType;
	private JTextField tfLocation;
	// private JTextArea tABibl, tAPlaDoc;
	private final ButtonGroup groupStatus = new ButtonGroup();
	private JComboBox cbTipusAula;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		LanguageUtils.setDefaultLanguage("i18n/messages");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClassroomSearchWindow frame = new ClassroomSearchWindow(
							new ClientConnectionHelper());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @param connectionHelper_
	 */
	public ClassroomSearchWindow(final ClientConnectionHelper connectionHelper_) {
		//setTitle("eAssistencia - Classroom Search");
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//setBounds(0, 242, 700, 658);
		// getContentPane().setLayout(null);
		setBorder(new TitledBorder(null,
				LanguageUtils
						.getMessage("main.menu.maintenance.class"),
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(null);

		this.connectionHelper_ = connectionHelper_;
		this.maintenance = connectionHelper_.getMaintenanceManager();

		
		//getContentPane().
		JPanel panelSup = new JPanel();
		panelSup.setBounds(0, 11, 619, 267);
		panelSup.setLayout(null);
		add(panelSup);//,BorderLayout.NORTH

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(6, 300, 619, 120);
		add(scrollPane);
		//scrollPane.setMaximumSize( new Dimension(200, 200));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(6, 450, 619, 37);
		add(panel_1);//,BorderLayout.SOUTH

		
		
		GridBagLayout gbl_panelSup = new GridBagLayout();
		gbl_panelSup.rowHeights = new int[] { 30, 30, 30, 30, 30, 30, 0 };
		gbl_panelSup.columnWidths = new int[] { 30, 90, 150, 100, 90, 90, 90 };
		gbl_panelSup.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0,
				0.0 };
		gbl_panelSup.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0 };
		panelSup.setLayout(gbl_panelSup);

		JLabel labelId = new JLabel(" Id");
		labelId.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelId = new GridBagConstraints();
		gbc_labelId.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelId.anchor = GridBagConstraints.WEST;
		gbc_labelId.insets = new Insets(0, 0, 5, 5);
		gbc_labelId.gridx = 1;
		gbc_labelId.gridy = 0;
		panelSup.add(labelId, gbc_labelId);

		tfId = new JTextField();
		GridBagConstraints gbc_tfId = new GridBagConstraints();
		gbc_tfId.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfId.insets = new Insets(0, 0, 5, 5);
		gbc_tfId.gridx = 2;
		gbc_tfId.gridy = 0;
		panelSup.add(tfId, gbc_tfId);
		tfId.setColumns(1);

		JLabel labelActivity = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelStatus"));
		labelActivity.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelActivity = new GridBagConstraints();
		gbc_labelActivity.anchor = GridBagConstraints.EAST;
		gbc_labelActivity.insets = new Insets(0, 0, 5, 5);
		gbc_labelActivity.gridx = 3;
		gbc_labelActivity.gridy = 0;
		panelSup.add(labelActivity, gbc_labelActivity);

		JLabel labelName = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelName"));
		labelName.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelName = new GridBagConstraints();
		gbc_labelName.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelName.anchor = GridBagConstraints.WEST;
		gbc_labelName.insets = new Insets(0, 0, 5, 5);
		gbc_labelName.gridx = 1;
		gbc_labelName.gridy = 1;
		panelSup.add(labelName, gbc_labelName);

		tfName = new JTextField();
		GridBagConstraints gbc_tfName = new GridBagConstraints();
		gbc_tfName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfName.insets = new Insets(0, 0, 5, 5);
		gbc_tfName.gridx = 2;
		gbc_tfName.gridy = 1;
		panelSup.add(tfName, gbc_tfName);
		tfName.setColumns(10);

		JRadioButton rdbtnAll = new JRadioButton("all");
		rdbtnAll.setBackground(UIManager.getColor("CheckBox.background"));
		groupStatus.add(rdbtnAll);
		GridBagConstraints gbc_rdbtnAll = new GridBagConstraints();
		gbc_rdbtnAll.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnAll.gridx = 3;
		gbc_rdbtnAll.gridy = 1;
		panelSup.add(rdbtnAll, gbc_rdbtnAll);

		JRadioButton rdbtnActive = new JRadioButton("active");
		rdbtnActive.setBackground(UIManager.getColor("CheckBox.background"));
		groupStatus.add(rdbtnActive);
		GridBagConstraints gbc_rdbtnActive = new GridBagConstraints();
		gbc_rdbtnActive.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnActive.gridx = 4;
		gbc_rdbtnActive.gridy = 1;
		panelSup.add(rdbtnActive, gbc_rdbtnActive);

		JRadioButton rdbtnInactive = new JRadioButton("inactive");
		rdbtnInactive.setBackground(UIManager.getColor("CheckBox.background"));
		groupStatus.add(rdbtnInactive);
		GridBagConstraints gbc_rdbtnInactive = new GridBagConstraints();
		gbc_rdbtnInactive.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnInactive.gridx = 5;
		gbc_rdbtnInactive.gridy = 1;
		panelSup.add(rdbtnInactive, gbc_rdbtnInactive);

		//Capacity
		JLabel labelCapacity = new JLabel(LanguageUtils.getMessage("client.maintenance.classmanag.labelCapacity"));
		labelCapacity.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelCapacity = new GridBagConstraints();
		gbc_labelCapacity.anchor = GridBagConstraints.WEST;
		gbc_labelCapacity.insets = new Insets(0, 0, 5, 5);
		gbc_labelCapacity.gridx = 1;
		gbc_labelCapacity.gridy = 2;
		panelSup.add(labelCapacity, gbc_labelCapacity);

		tfCapacity = new JTextField();
		GridBagConstraints gbc_tfReqAttend = new GridBagConstraints();
		gbc_tfReqAttend.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfReqAttend.insets = new Insets(0, 0, 5, 5);
		gbc_tfReqAttend.gridx = 2;
		gbc_tfReqAttend.gridy = 2;
		panelSup.add(tfCapacity, gbc_tfReqAttend);
		tfCapacity.setColumns(10);

		//Type
		JLabel labelTypeOfClassroom = new JLabel(LanguageUtils.getMessage("client.maintenance.classmanag.labelType"));
		labelTypeOfClassroom.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelTypeOfClassroom = new GridBagConstraints();
		gbc_labelTypeOfClassroom.anchor = GridBagConstraints.WEST;
		gbc_labelTypeOfClassroom.insets = new Insets(0, 0, 5, 5);
		gbc_labelTypeOfClassroom.gridx = 1;
		gbc_labelTypeOfClassroom.gridy = 3;
		panelSup.add(labelTypeOfClassroom, gbc_labelTypeOfClassroom);

		cbTipusAula = new JComboBox();
		GridBagConstraints gbc_cbTipusAula = new GridBagConstraints();
		gbc_cbTipusAula.insets = new Insets(0, 0, 5, 5);
		gbc_cbTipusAula.fill = GridBagConstraints.HORIZONTAL;
		gbc_cbTipusAula.gridx = 2;
		gbc_cbTipusAula.gridy = 3;
		panelSup.add(cbTipusAula, gbc_cbTipusAula);
		cbTipusAula.addItem("---");				
		cbTipusAula.addItem(TipusAula.Laboratori);				
		cbTipusAula.addItem(TipusAula.Taller);				

		JLabel labelLocation = new JLabel(LanguageUtils.getMessage("client.maintenance.classmanag.labelLocation"));
		labelLocation.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelLocation = new GridBagConstraints();
		gbc_labelLocation.anchor = GridBagConstraints.WEST;
		gbc_labelLocation.insets = new Insets(0, 0, 5, 5);
		gbc_labelLocation.gridx = 1;
		gbc_labelLocation.gridy = 4;
		panelSup.add(labelLocation, gbc_labelLocation);

		tfLocation = new JTextField();
		tfLocation.setColumns(10);
		GridBagConstraints gbc_tfLocation = new GridBagConstraints();
		gbc_tfLocation.insets = new Insets(0, 0, 5, 5);
		gbc_tfLocation.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfLocation.gridx = 2;
		gbc_tfLocation.gridy = 4;
		panelSup.add(tfLocation, gbc_tfLocation);

		JButton btnSearch = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnSearch"));
		GridBagConstraints gbc_btnSearch = new GridBagConstraints();
		gbc_btnSearch.fill = GridBagConstraints.BOTH;
		gbc_btnSearch.insets = new Insets(0, 0, 0, 5);
		gbc_btnSearch.gridx = 1;
		gbc_btnSearch.gridy = 6;
		panelSup.add(btnSearch, gbc_btnSearch);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					listClassroom();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		});


		JButton btnClear = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnClear"));
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tfCapacity.setText("");
				//tfClassroomType.setText("");
				tfId.setText("");
				tfLocation.setText("");
				tfName.setText("");
				cbTipusAula.setSelectedIndex(0);
				groupStatus.clearSelection();
			}
		});
		
		GridBagConstraints gbc_btnClear = new GridBagConstraints();
		gbc_btnClear.fill = GridBagConstraints.BOTH;
		gbc_btnClear.insets = new Insets(0, 0, 0, 5);
		gbc_btnClear.gridx = 2;
		gbc_btnClear.gridy = 6;
		panelSup.add(btnClear, gbc_btnClear);

		JButton btnExit = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnExit"));
		btnExit.setForeground(Color.RED);
		GridBagConstraints gbc_btnExit = new GridBagConstraints();
		gbc_btnExit.insets = new Insets(0, 0, 0, 5);
		gbc_btnExit.fill = GridBagConstraints.BOTH;
		gbc_btnExit.anchor = GridBagConstraints.WEST;
		gbc_btnExit.gridx = 5;
		gbc_btnExit.gridy = 6;
		panelSup.add(btnExit, gbc_btnExit);


		tbClassroomList = new JTable();
		//tbClassroomList.setEnabled(false);
		tbClassroomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tbClassroomList.setModel(new DefaultTableModel(new Object[][] {}, columnNames));		
		scrollPane.add(tbClassroomList);
		scrollPane.setViewportView(tbClassroomList);
		
		
		
		JButton btnCreate = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnCreate"));
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClassroomEditWindow createWindow = new ClassroomEditWindow(connectionHelper_);
				createWindow.setVisible(true);
			}
		});
		panel_1.add(btnCreate);

		
		
		JButton btnEdit = new JButton(LanguageUtils.getMessage("client.maintenance.classmanag.btnEdit"));
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int i = tbClassroomList.getSelectedRow();
				TableModel model2 = tbClassroomList.getModel();
				Integer identificador = Integer.valueOf( model2.getValueAt(i, 0).toString());
				try {
					Aula classroom = maintenance.findClassroomById(identificador);
					ClassroomEditWindow createWindow = new ClassroomEditWindow(classroom, connectionHelper_);
					createWindow.setVisible(true);				
					
				} catch (RemoteException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				} catch (RuntimeException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				}
				
			}
		});
		panel_1.add(btnEdit);
		
		

	
	}

	
	Object[] columnNames = new String[] {
			LanguageUtils.getMessage("classroom.id"),
			LanguageUtils.getMessage("classroom.name"),
			LanguageUtils.getMessage("classroom.location"),
			LanguageUtils.getMessage("classroom.type"),
			LanguageUtils.getMessage("classroom.capacity")
			};
	
	protected void listClassroom() throws RemoteException{
		System.out.println("listClassroom");
		
		Aula classroomX=new Aula();
		//usuari.setContrasenya(txtPassword.getText());
		classroomX.setIdAula("".equals(tfId.getText())?0:Integer.valueOf(tfId.getText()));
		classroomX.setNom("".equals(tfName.getText())?null:tfName.getText());
		classroomX.setCapacitat("".equals(tfCapacity.getText())?0:Integer.valueOf(tfCapacity.getText()));
		classroomX.setLocalitzacio("".equals(tfLocation.getText())?null:tfLocation.getText());
		classroomX.setTipusAula("---".equals(cbTipusAula.getSelectedItem())?null:(TipusAula)cbTipusAula.getSelectedItem());
		classroomX.setActive(getSelection(groupStatus)==null || "all".equals(getSelection(groupStatus).getText())?null:"active".equals(getSelection(groupStatus).getText()));

		
		List<Aula> classrooms = this.maintenance.findClassrooms(classroomX);

		
		Object[][] data = new Object[classrooms.size()][columnNames.length];

		for (int i = 0; i < data.length; i++) {
			Aula classroom = classrooms.get(i);
			data[i] = new Object[] { classroom.getIdAula(), classroom.getNom(),
					classroom.getLocalitzacio(), classroom.getTipusAula(),
					classroom.getCapacitat() };
		}

		TableModel model = new DefaultTableModel(data, columnNames);
		tbClassroomList.setModel(model);
		
		
	}
	  public static JRadioButton getSelection(ButtonGroup group) {
		for (Enumeration e = group.getElements(); e.hasMoreElements();) {
			JRadioButton b = (JRadioButton) e.nextElement();
			if (b.getModel() == group.getSelection()) {
				return b;
			}
		}
		return null;
	  }

	
	

}
